package com.room.search.web.rest;

import com.room.search.repository.RoomRentBasicRepository;
import com.room.search.service.RoomRentBasicService;
import com.room.search.service.dto.RoomRentBasicDTO;
import com.room.search.service.mapper.RoomRentBasicMapper;
import com.room.search.web.rest.errors.BadRequestAlertException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import tech.jhipster.web.util.HeaderUtil;
import tech.jhipster.web.util.ResponseUtil;

/**
 * REST controller for managing {@link com.room.search.domain.RoomRentBasic}.
 */
@RestController
@RequestMapping("/api")
public class RoomRentBasicResource {

    private final Logger log = LoggerFactory.getLogger(RoomRentBasicResource.class);

    private static final String ENTITY_NAME = "roomRentBasic";

    @Value("${jhipster.clientApp.name}")
    private String applicationName;

    private final RoomRentBasicService roomRentBasicService;

    private final RoomRentBasicRepository roomRentBasicRepository;
    private final RoomRentBasicMapper rentBasicMapper;

    public RoomRentBasicResource(RoomRentBasicService roomRentBasicService,
                                 RoomRentBasicRepository roomRentBasicRepository,
                                 RoomRentBasicMapper rentBasicMapper) {
        this.roomRentBasicService = roomRentBasicService;
        this.roomRentBasicRepository = roomRentBasicRepository;
        this.rentBasicMapper = rentBasicMapper;
    }

    /**
     * {@code POST  /room-rent-basics} : Create a new roomRentBasic.
     *
     * @param roomRentBasicDTO the roomRentBasicDTO to create.
     * @return the {@link ResponseEntity} with status {@code 201 (Created)} and with body the new roomRentBasicDTO, or with status {@code 400 (Bad Request)} if the roomRentBasic has already an ID.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PostMapping("/room-rent-basics")
    public ResponseEntity<RoomRentBasicDTO> createRoomRentBasic(@Valid @RequestBody RoomRentBasicDTO roomRentBasicDTO)
        throws URISyntaxException {
        log.debug("REST request to save RoomRentBasic : {}", roomRentBasicDTO);
        if (roomRentBasicDTO.getId() != null) {
            throw new BadRequestAlertException("A new roomRentBasic cannot already have an ID", ENTITY_NAME, "idexists");
        }
        RoomRentBasicDTO result = rentBasicMapper.toDto(roomRentBasicService.save(roomRentBasicDTO));
        return ResponseEntity
            .created(new URI("/api/room-rent-basics/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(applicationName, false, ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * {@code PUT  /room-rent-basics/:id} : Updates an existing roomRentBasic.
     *
     * @param id the id of the roomRentBasicDTO to save.
     * @param roomRentBasicDTO the roomRentBasicDTO to update.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated roomRentBasicDTO,
     * or with status {@code 400 (Bad Request)} if the roomRentBasicDTO is not valid,
     * or with status {@code 500 (Internal Server Error)} if the roomRentBasicDTO couldn't be updated.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PutMapping("/room-rent-basics/{id}")
    public ResponseEntity<RoomRentBasicDTO> updateRoomRentBasic(
        @PathVariable(value = "id", required = false) final Long id,
        @Valid @RequestBody RoomRentBasicDTO roomRentBasicDTO
    ) throws URISyntaxException {
        log.debug("REST request to update RoomRentBasic : {}, {}", id, roomRentBasicDTO);
        if (roomRentBasicDTO.getId() == null) {
            throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
        }
        if (!Objects.equals(id, roomRentBasicDTO.getId())) {
            throw new BadRequestAlertException("Invalid ID", ENTITY_NAME, "idinvalid");
        }

        if (!roomRentBasicRepository.existsById(id)) {
            throw new BadRequestAlertException("Entity not found", ENTITY_NAME, "idnotfound");
        }

        RoomRentBasicDTO result = rentBasicMapper.toDto(roomRentBasicService.save(roomRentBasicDTO));
        return ResponseEntity
            .ok()
            .headers(HeaderUtil.createEntityUpdateAlert(applicationName, false, ENTITY_NAME, roomRentBasicDTO.getId().toString()))
            .body(result);
    }

    /**
     * {@code PATCH  /room-rent-basics/:id} : Partial updates given fields of an existing roomRentBasic, field will ignore if it is null
     *
     * @param id the id of the roomRentBasicDTO to save.
     * @param roomRentBasicDTO the roomRentBasicDTO to update.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated roomRentBasicDTO,
     * or with status {@code 400 (Bad Request)} if the roomRentBasicDTO is not valid,
     * or with status {@code 404 (Not Found)} if the roomRentBasicDTO is not found,
     * or with status {@code 500 (Internal Server Error)} if the roomRentBasicDTO couldn't be updated.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PatchMapping(value = "/room-rent-basics/{id}", consumes = { "application/json", "application/merge-patch+json" })
    public ResponseEntity<RoomRentBasicDTO> partialUpdateRoomRentBasic(
        @PathVariable(value = "id", required = false) final Long id,
        @NotNull @RequestBody RoomRentBasicDTO roomRentBasicDTO
    ) throws URISyntaxException {
        log.debug("REST request to partial update RoomRentBasic partially : {}, {}", id, roomRentBasicDTO);
        if (roomRentBasicDTO.getId() == null) {
            throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
        }
        if (!Objects.equals(id, roomRentBasicDTO.getId())) {
            throw new BadRequestAlertException("Invalid ID", ENTITY_NAME, "idinvalid");
        }

        if (!roomRentBasicRepository.existsById(id)) {
            throw new BadRequestAlertException("Entity not found", ENTITY_NAME, "idnotfound");
        }

        Optional<RoomRentBasicDTO> result = roomRentBasicService.partialUpdate(roomRentBasicDTO);

        return ResponseUtil.wrapOrNotFound(
            result,
            HeaderUtil.createEntityUpdateAlert(applicationName, false, ENTITY_NAME, roomRentBasicDTO.getId().toString())
        );
    }

    /**
     * {@code GET  /room-rent-basics} : get all the roomRentBasics.
     *
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of roomRentBasics in body.
     */
    @GetMapping("/room-rent-basics")
    public List<RoomRentBasicDTO> getAllRoomRentBasics() {
        log.debug("REST request to get all RoomRentBasics");
        return roomRentBasicService.findAll();
    }

    /**
     * {@code GET  /room-rent-basics/:id} : get the "id" roomRentBasic.
     *
     * @param id the id of the roomRentBasicDTO to retrieve.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the roomRentBasicDTO, or with status {@code 404 (Not Found)}.
     */
    @GetMapping("/room-rent-basics/{id}")
    public ResponseEntity<RoomRentBasicDTO> getRoomRentBasic(@PathVariable Long id) {
        log.debug("REST request to get RoomRentBasic : {}", id);
        Optional<RoomRentBasicDTO> roomRentBasicDTO = roomRentBasicService.findOne(id);
        return ResponseUtil.wrapOrNotFound(roomRentBasicDTO);
    }

    /**
     * {@code DELETE  /room-rent-basics/:id} : delete the "id" roomRentBasic.
     *
     * @param id the id of the roomRentBasicDTO to delete.
     * @return the {@link ResponseEntity} with status {@code 204 (NO_CONTENT)}.
     */
    @DeleteMapping("/room-rent-basics/{id}")
    public ResponseEntity<Void> deleteRoomRentBasic(@PathVariable Long id) {
        log.debug("REST request to delete RoomRentBasic : {}", id);
        roomRentBasicService.delete(id);
        return ResponseEntity
            .noContent()
            .headers(HeaderUtil.createEntityDeletionAlert(applicationName, false, ENTITY_NAME, id.toString()))
            .build();
    }
}
